package com.md.model.word;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFRelation;
import org.apache.xmlbeans.XmlException;

import java.io.IOException;
import java.util.List;

/**
 * @author 刘骄阳
 * 2024-06-14 14:18
 */
@Slf4j
public abstract class BaseDocumentStyle {
  private final XWPFRelation type;

  public BaseDocumentStyle(final XWPFRelation type) {
    this.type = type;
  }

  protected abstract void setPartList(POIXMLDocumentPart documentPart) throws IOException, XmlException;


  private boolean isType(final String relationshipType) {
    return type.getRelation().equals(relationshipType);
  }

  public void initStyleList(final XWPFDocument document) {
    List<POIXMLDocumentPart.RelationPart> relationParts = document.getRelationParts();
    relationParts.stream().filter(relationPart ->
      isType(relationPart.getRelationship().getRelationshipType())).forEach(relationPart -> {
      try {
        setPartList(relationPart.getDocumentPart());
      } catch (IOException | XmlException e) {
        log.error("追加 partList 失败: {}", e.getMessage(), e);
      }
    });
  }
}
